package com.shankungfu.main;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.shankungfu.R;
import com.shankungfu.util.ToastUtil;

public class RegisterActivity extends Activity implements OnClickListener {
	
	public static final int REGISTER = 0;
	public static final int REGISTER_OK = 1;
	public static final int REGISTER_FAIL = 2;
	public static final String USERDATA = "USERDATA";
	private EditText username;
	private EditText email;
	private EditText password;
	private EditText sure_password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_layout);
		
		Button bt = (Button) findViewById(R.id.goback);
		username = (EditText) findViewById(R.id.register_username);
		email = (EditText) findViewById(R.id.register_email);
		password = (EditText) findViewById(R.id.register_password);
		sure_password = (EditText) findViewById(R.id.register_sure_password);
		
		Button nextActivity = (Button) findViewById(R.id.next_activity);
		bt.setOnClickListener(this);
		nextActivity.setOnClickListener(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == REGISTER && resultCode == REGISTER_OK) {
			finish();
		} else if (requestCode == REGISTER && resultCode == REGISTER_FAIL) {
			ToastUtil.showMessageText(RegisterActivity.this, "请重新输入注册信息");
		}
		
	}
	
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.goback:
			finish();
			break;
		case R.id.next_activity:
			if (!verifText()) {
				return ;
			}
			
			ArrayList<String> list = new ArrayList<String>();
			list.add(username.getText().toString());
			list.add(email.getText().toString());
			list.add(password.getText().toString());
			
			Intent intent = new Intent(this, RegisterNextActivity.class);
			intent.putStringArrayListExtra(USERDATA, list);
			startActivityForResult(intent, REGISTER);
			break;
		}
		
	}

	private boolean verifText() {
		boolean isVerif = true;
		String text = "";
		String emailStr = email.getText().toString();
		String passwordStr = password.getText().toString();
		String surePasswordStr = sure_password.getText().toString();
		
		if (TextUtils.isEmpty(username.getText().toString())) {
			text = "用户名为空";
			isVerif = false;
		}
		if (TextUtils.isEmpty(emailStr)) {
			text = "邮箱为空";
			isVerif = false;
		}
		
		if (!emailFormat(emailStr)) { //判断格式
			text = "邮箱格式不正确";
			isVerif = false;
         }
		if (TextUtils.isEmpty(passwordStr)) {
			text = "密码为空";
			isVerif = false;
		}
		if (TextUtils.isEmpty(surePasswordStr)) {
			text = "确认密码为空";
			isVerif = false;
		}
		if (!passwordStr.equals(surePasswordStr)) {
			text = "密码和确认密码不一致";
			isVerif = false;
		}
		
		if (!TextUtils.isEmpty(text)) {
			ToastUtil.showMessageText(RegisterActivity.this, text);
		}
		return isVerif;
	}
	    /**
	     * 验证输入的邮箱格式是否符合
	     * @param email
	     * @return 是否合法
	     */
	public boolean emailFormat(String email)
	    {
	        boolean tag = true;
	        final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
	        final Pattern pattern = Pattern.compile(pattern1);
	        final Matcher mat = pattern.matcher(email);
	        if (!mat.find()) {
	            tag = false;
	        }
	        return tag;
	    }
	
}
